package cn.volunteer.points.system.controller;


import cn.volunteer.points.system.annotation.AdminLoginToken;
import cn.volunteer.points.system.annotation.LoginToken;
import cn.volunteer.points.system.annotation.PassToken;
import cn.volunteer.points.system.api.REST_URL;
import cn.volunteer.points.system.pojo.entity.Password;
import cn.volunteer.points.system.pojo.entity.SearchValue;
import cn.volunteer.points.system.pojo.entity.User;
import cn.volunteer.points.system.pojo.entity.UserSearchVO;
import cn.volunteer.points.system.pojo.qo.LoginQO;
import cn.volunteer.points.system.pojo.qo.RegisterQO;
import cn.volunteer.points.system.pojo.vo.UserVO;
import cn.volunteer.points.system.service.UserService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @description: 用户api接口
 * @author:
 * @create: 创建时间：2021-04-16
 **/
@RestController
@Slf4j
public class UserController extends RestfulController{


    @Autowired
    private UserService userService ;


    @ApiOperation("用户登录")
    @PostMapping(path = REST_URL.USER_LOGIN)
    @PassToken
    public Response<?> login(@RequestBody LoginQO loginQO){
        UserVO login = userService.login(loginQO);
        return success(login,"登录成功") ;
    }

    @ApiOperation("用户注销")
    @PostMapping(path = REST_URL.USER_LOGOUT)
    @LoginToken
    public Response<?> logout(){
        return null ;
    }

    @ApiOperation("用户注册")
    @PostMapping(path = REST_URL.USER_ADD)
    @PassToken
    public Response<?> addUser(@RequestBody RegisterQO user){
        String addUser = userService.addUser(user);
        return success(user,addUser) ;
    }

    @ApiOperation("修改用户信息")
    @PostMapping(path = REST_URL.USER_UPDATE)
    @LoginToken
    public Response<?> updateUser(@RequestBody User user){
        String updateUser = userService.updateUser(user);
        user.setId(user.getUserId());
        return success(user,updateUser) ;
    }

    @ApiOperation("查看用户")
    @PostMapping(path = REST_URL.USER_INFO)
    @LoginToken
    public Response<?> userInfo(@RequestBody UserSearchVO user){
        UserVO byId = userService.findById(user.getUserId());
        return success(byId) ;
    }


    @ApiOperation("管理员修改用户信息")
    @PostMapping(path = REST_URL.ADMIN_USER_UPDATE)
    @AdminLoginToken
    public Response<?> updateUserAdmin(@RequestBody User user){
        String updateUser = userService.updateUserAdmin(user);
        user.setId(user.getUserId());
        return success(user,updateUser) ;
    }

    @ApiOperation("修改密码")
    @PostMapping(path = REST_URL.CHANGE_PASSWORD)
    @LoginToken
    public Response<?> updateUserPassword(@RequestBody Password password){
        JSONObject jsonObject = userService.updatePassword(password);

        Response<?> respResult = new Response();
        respResult.setResponseTime(new Date());
        respResult.setCode(jsonObject.getInteger("code"));
        respResult.setMsg(jsonObject.getString("msg"));
        return respResult ;
    }

    @ApiOperation("删除用户")
    @PostMapping(path = REST_URL.USER_DELETE)
    @AdminLoginToken
    public Response<?> delUser(@RequestBody User user){
        Integer id = user.getId();
        userService.delUser(id);
        return success(id,"用户删除成功！！！") ;
    }


    @ApiOperation("用户列表")
    @PostMapping(path = REST_URL.USER_LIST)
    @AdminLoginToken
    public Response<?> userList(@RequestBody SearchValue searchValue){
        String searchValue1 = searchValue.getSearchValue();
        List<UserVO> users = userService.userLisr(searchValue1);
        return success(users) ;
    }




}
